<?php 
$name = null;
$firstname = null;
$mail = null;

$errors = array();

if(isset($_SESSION['idUser']))
{	
	$user = User::read($_SESSION['idUser']);
	
	if(isset($_POST['edit_profil'])) //Editing profil
	{
		
		$user->setNameUser($_POST['name']);
		$user->setFirstnameUser($_POST['firstname']);
		
		if(checkEmailAddress($_POST['mail']))
		{
			$user->setMailUser($_POST['mail']);
			echo '<label>Profil &eacute;dit&eacute; avec succ&egrave;s.</label> <br />';
		}
		else	
			array_push($errors, "E-mail non valide !");	
	}
	
	if(isset($_POST['edit_pwd']))
	{
		if(isset($_POST['old_pwd'])) //Trying to change pwd
		{
			if($user->getPwdUser()==sha1($_POST['old_pwd']))
			{
				if(5 < strlen($_POST['new_pwd1']))
				{
					if(isset($_POST['new_pwd1']) && isset($_POST['new_pwd1']))
					{
						if($_POST['new_pwd1'] == $_POST['new_pwd1'])
						{
							$user->setPwdUser(sha1(trim($_POST['new_pwd1'])));
							echo '<label>Mot de passe &eacute;dit&eacute; avec succ&egrave;s.</label> <br />';
						}
						else
							array_push($errors, "Vous devez confirmer correctement le nouveau mot de passe.");
					}
					else
						array_push($errors, "Vous devez indiquer le nouveau mot de passe puis le confirmer.");
				}
				else
					array_push($errors, "Le nouveau mot de passe doit contenir plus de 5 caract&egrave;res.");
			}
			else
				array_push($errors, "Ancien mot de passe incorrect.");
		}
	}
	
	if(0 == count($errors))
	{
		$user->save();
		
	}
	else
	{
		foreach($errors AS $error) 
		{	// Display errors
			echo '<label>'.$error.'</label> <br />';
		}
	}
	

	$user = User::read($_SESSION['idUser']);
	$name= $user->getNameUser();
	$firstname= $user->getFirstnameUser();
	$mail= $user->getMailUser();
}
else
	redirige("index.php");

	include("partials/profil.php");
?>